package com.gy.boot.common.check;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import com.gy.boot.common.dao.IEntity;
import com.gy.boot.common.exception.CheckException;
import com.gy.boot.common.util.BlankUtil;
import com.gy.boot.common.util.NumberTool;

import java.util.List;

public class CheckUtil {

    /**
     * 校验全部参数, 报错一块返回
     * */
    public static void checkParamsAll(IEntity params, List<CheckEntity> listCheckEntity) {
        if (listCheckEntity == null || listCheckEntity.isEmpty()) {
            return;
        }
        StringBuilder errSB = new StringBuilder();
        for (CheckEntity checkEntity : listCheckEntity) {
            try {
                checkParams(params, checkEntity);
            } catch (CheckException ce) {
                errSB.append(ce.getMessage()).append("; ");
            }
        }
        String errMsg = errSB.toString();
        if (!BlankUtil.isBlank(errMsg)) {
            throw new CheckException(errMsg);
        }
    }

    /**
     * 校验参数, 报错立即返回
     * */
    public static void checkParams(IEntity params, List<CheckEntity> listCheckEntity) {
        if (listCheckEntity == null || listCheckEntity.isEmpty()) {
            return;
        }
        for (CheckEntity checkEntity : listCheckEntity) {
            checkParams(params, checkEntity);
        }
    }

    /**
     * 校验数据
     * @param params 参数
     * @param checkEntity 校验规则
     */
    public static void checkParams(IEntity params, CheckEntity checkEntity) {
        String code = checkEntity.getCode();
        Object value = params.get(code); // 参数值
        checkParams(value, checkEntity);
    }

    /**
     * 校验参数值
     * @param value 参数值
     * @param checkEntity 校验规则
     */
    public static void checkParams(Object value, CheckEntity checkEntity) {
        if (checkEntity == null) {
            return;
        }
        CheckEnum checkEnum = checkEntity.getCheckEnum();
        switch (checkEnum) {
            case NOT_NULL: // 不能为空
                System.out.println("NOT_NULL");
                if (value == null) {
                    String errMsg = checkEntity.getErrMsg("不能为空");
                    throw new CheckException(errMsg);
                }
                break;
            case NOT_BLANK: // 不能为空白字符串
                System.out.println("NOT_BLANK");
                if (BlankUtil.isBlank(value)) {
                    String errMsg = checkEntity.getErrMsg("不能为空白");
                    throw new CheckException(errMsg);
                }
                break;
            case IS_INTEGER: // Integer 类型
                System.out.println("IS_INTEGER");
                boolean isNotInt = value != null && !NumberUtil.isInteger(value.toString());
                if (isNotInt) {
                    String errMsg = checkEntity.getErrMsg("必须为整数类型");
                    throw new CheckException(errMsg);
                }
                break;
            case IS_int: // Integer 类型
                System.out.println("IS_int");
                boolean isNotI = value == null || !NumberUtil.isInteger(value.toString());
                if (isNotI) {
                    String errMsg = checkEntity.getErrMsg("必须为整数类型");
                    throw new CheckException(errMsg);
                }
                break;
            case IS_LONG: // Long 类型
                System.out.println("IS_LONG");
                boolean isNotLong = value != null && !NumberUtil.isLong(value.toString());
                if (isNotLong) {
                    String errMsg = checkEntity.getErrMsg("必须为长整数类型");
                    throw new CheckException(errMsg);
                }
                break;
            case IS_long: // long 类型
                System.out.println("IS_long");
                boolean isNotL = value == null || !NumberUtil.isLong(value.toString());
                if (isNotL) {
                    String errMsg = checkEntity.getErrMsg("必须为长整数类型");
                    throw new CheckException(errMsg);
                }
                break;
            case IS_DOUBLE: // Double 类型
                System.out.println("IS_DOUBLE");
                boolean isNotDouble = value != null && NumberTool.getDouble(value) == null;
                if (isNotDouble) {
                    String errMsg = checkEntity.getErrMsg("必须为数字类型");
                    throw new CheckException(errMsg);
                }
                break;
            case IS_double: // double 类型
                System.out.println("IS_double");
                boolean isNotD = value == null || NumberTool.getDouble(value) == null;
                if (isNotD) {
                    String errMsg = checkEntity.getErrMsg("必须为数字类型");
                    throw new CheckException(errMsg);
                }
                break;
            case MIN_VAL: // 最小值
                System.out.println("MIN_VAL");
                Double valueD = NumberTool.getDouble(value);
                if (valueD == null) {
                    return;
                }
                Double checkValueD = checkEntity.getCheckValueDouble();
                if (checkValueD == null) {
                    return;
                }
                if (valueD < checkValueD) {
                    String errMsg = checkEntity.getErrMsg("小于"+checkEntity.getCheckValueStr());
                    throw new CheckException(errMsg);
                }
                break;
            case MAX_VAL: // 最大值
                System.out.println("MIN_VAL");
                Double valueD2 = NumberTool.getDouble(value);
                if (valueD2 == null) {
                    return;
                }
                Double checkValueD2 = checkEntity.getCheckValueDouble();
                if (checkValueD2 == null) {
                    return;
                }
                if (valueD2 > checkValueD2) {
                    String errMsg = checkEntity.getErrMsg("大于"+checkEntity.getCheckValueStr());
                    throw new CheckException(errMsg);
                }
                break;
            case MIN_LENGTH: // 最小长度
                System.out.println("MIN_LENGTH");
                if (value == null) {
                    return;
                }
                Integer checkValueIntMin = checkEntity.getCheckValueInt();
                if (checkValueIntMin == null) {
                    return;
                }
                if (value.toString().length() < checkValueIntMin) {
                    String errMsg = checkEntity.getErrMsg("长度小于"+checkValueIntMin);
                    throw new CheckException(errMsg);
                }
                break;
            case MAX_LENGTH: // 最大长度
                System.out.println("MAX_LENGTH");
                if (value == null) {
                    return;
                }
                Integer checkValueIntMax = checkEntity.getCheckValueInt();
                if (checkValueIntMax == null) {
                    return;
                }
                if (value.toString().length() > checkValueIntMax) {
                    String errMsg = checkEntity.getErrMsg("长度大于"+checkValueIntMax);
                    throw new CheckException(errMsg);
                }
                break;
            case IS_PHONE: // 手机号格式
                System.out.println("IS_PHONE");
                if (BlankUtil.isBlank(value)) {
                    return;
                }
                boolean isMobile = Validator.isMobile(value.toString());
                if (!isMobile) {
                    String errMsg = checkEntity.getErrMsg("必须为手机号格式");
                    throw new CheckException(errMsg);
                }
                break;
            case IS_EMAIL: // 电子邮件格式
                System.out.println("IS_EMAIL");
                if (BlankUtil.isBlank(value)) {
                    return;
                }
                boolean isEmpty = Validator.isEmpty(value.toString());
                if (!isEmpty) {
                    String errMsg = checkEntity.getErrMsg("必须为电子邮件格式");
                    throw new CheckException(errMsg);
                }
                break;
            case IS_EQUAL: // 是否相等
                System.out.println("IS_EQUAL");
                if (BlankUtil.isBlank(value)) {
                    return;
                }
                String checkValueStr = checkEntity.getCheckValueStr();
                if (BlankUtil.isBlank(checkValueStr)) {
                    return;
                }
                if (!value.equals(checkValueStr)) {
                    String errMsg = checkEntity.getErrMsg("参数值错误");
                    throw new CheckException(errMsg);
                }
                break;
            case IS_EQUAL_ARR: // 元素是否相等
                System.out.println("IS_EQUAL");
                if (BlankUtil.isBlank(value)) {
                    return;
                }
                String[] checkValueArr = checkEntity.getCheckValueArr();
                if (checkValueArr == null || checkValueArr.length == 0) {
                    return;
                }
                boolean contains = ArrayUtil.contains(checkValueArr, value);
                if (!contains) {
                    String errMsg = checkEntity.getErrMsg("参数值错误");
                    throw new CheckException(errMsg);
                }
                break;
            default:
                System.out.println("没有该条件");
        }
    }
}
